{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2.1 神经网络的推理全貌图"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:27:51.614454700Z",
     "start_time": "2023-05-03T11:27:51.523453Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "W1 = np.random.randn(2, 4)  # 权重"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:28:13.217065900Z",
     "start_time": "2023-05-03T11:28:13.194015Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "b1 = np.random.randn(4)  # 偏置"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:28:37.820355600Z",
     "start_time": "2023-05-03T11:28:37.803378Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "x = np.random.randn(10, 2)  # 输入"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:29:27.111247100Z",
     "start_time": "2023-05-03T11:29:27.093836900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "h = np.dot(x, W1) + b1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:29:43.907592200Z",
     "start_time": "2023-05-03T11:29:43.892591600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1.60837087, -3.39057061,  3.09047977,  0.28817937],\n       [-0.02378698, -0.65398261,  0.92482973, -0.48319455],\n       [ 0.43285858, -0.09577748,  0.65270635, -1.28258006],\n       [ 3.4743166 ,  2.32926308, -0.30228468, -5.61540711],\n       [-0.63874938,  1.60664981, -0.70662435, -1.71671483],\n       [ 2.09413503, -2.2735667 ,  2.45399409, -0.96340204],\n       [ 1.245135  , -2.69290823,  2.54972843,  0.04853937],\n       [ 1.7897019 ,  0.54391642,  0.51992492, -2.87645427],\n       [ 3.16577703, -1.10961195,  1.91223228, -2.72739633],\n       [-0.66198494, -1.04229742,  1.04527229,  0.33353949]])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:30:07.661201800Z",
     "start_time": "2023-05-03T11:30:07.651200900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    return 1 / (1 + np.exp(-x))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:30:46.072217500Z",
     "start_time": "2023-05-03T11:30:46.057218700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "x = np.random.randn(10, 2)\n",
    "W1 = np.random.randn(2, 4)\n",
    "b1 = np.random.randn(4)\n",
    "W2 = np.random.randn(4, 3)\n",
    "b2 = np.random.randn(3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:30:59.552030300Z",
     "start_time": "2023-05-03T11:30:59.539029200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "h = np.dot(x, W1) + b1\n",
    "a = sigmoid(h)\n",
    "s = np.dot(a, W2) + b2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:31:14.512533700Z",
     "start_time": "2023-05-03T11:31:14.507533300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1.28112011, -0.93665197, -2.47111098],\n       [ 1.17426483, -0.74907712, -2.68382635],\n       [ 0.28383105,  1.20548114, -2.99152416],\n       [ 0.3314855 ,  1.06450999, -1.83873771],\n       [ 0.65463768,  0.07792789, -3.28431687],\n       [ 0.87754789,  0.4077345 , -2.36610468],\n       [ 1.39352544, -0.86280201, -2.20167306],\n       [ 0.99957774, -0.25240902, -2.78039054],\n       [ 0.50874131,  0.97548806, -1.34341042],\n       [ 1.24436984, -0.83601081, -2.514161  ]])"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T11:31:16.536019Z",
     "start_time": "2023-05-03T11:31:16.532018300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2.2 层的类化及正向传播的实现"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "因为这里只考虑正向传播，所以我们仅关注代码规范中的以下两点：一\n",
    "是在层中实现 forward() 方法；二是将参数整理到实例变量 params 中。\n",
    "\"\"\""
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "class Sigmoid:\n",
    "    def __init__(self):\n",
    "        self.params = []\n",
    "\n",
    "    def forward(self, x):\n",
    "        return 1 / (1 + np.exp(-x))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T12:14:06.237793700Z",
     "start_time": "2023-05-03T12:14:06.223793400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 全连接层 Affine 层"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "class Affine:\n",
    "    def __init__(self, W, b):\n",
    "        self.params = [W, b]\n",
    "\n",
    "    def forward(self, x):\n",
    "        W, b = self.params\n",
    "        out = np.dot(x, W) + b\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T12:17:32.817187800Z",
     "start_time": "2023-05-03T12:17:32.788998200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "class TwoLayerNet:\n",
    "    def __init__(self, input_size, hidden_size, output_size):\n",
    "        I, H, O = input_size, hidden_size, output_size\n",
    "\n",
    "        # 初始化权重和偏置\n",
    "        W1 = np.random.randn(I, H)\n",
    "        b1 = np.random.randn(H)\n",
    "        W2 = np.random.randn(H, O)\n",
    "        b2 = np.random.randn(O)\n",
    "\n",
    "        # 生成层\n",
    "        self.layers = [\n",
    "            Affine(W1, b1),\n",
    "            Sigmoid(),\n",
    "            Affine(W2, b2)\n",
    "        ]\n",
    "\n",
    "        # 将所有的权重整理到列表中\n",
    "        self.params = []\n",
    "        for layer in self.layers:\n",
    "            self.params += layer.params\n",
    "\n",
    "    def predict(self, x):\n",
    "        for layer in self.layers:\n",
    "            x = layer.forward(x)\n",
    "        return x"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T13:31:41.897556500Z",
     "start_time": "2023-05-03T13:31:41.883538800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "x = np.random.randn(10, 2)\n",
    "model = TwoLayerNet(2, 4, 3)\n",
    "s = model.predict(x)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T13:31:42.321020400Z",
     "start_time": "2023-05-03T13:31:42.314024300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-0.02108123,  3.41025848,  3.24252151],\n       [-0.20665381,  1.14886442,  2.96906862],\n       [-0.37793507,  2.7561923 ,  3.46813863],\n       [ 0.64097437,  0.80993855,  2.15040147],\n       [-0.10141782,  3.25453281,  3.22653282],\n       [ 0.10389036,  2.8603612 ,  2.8592935 ],\n       [ 0.1487155 ,  1.09256568,  2.46307406],\n       [-0.38884052,  3.0932895 ,  3.62255734],\n       [-0.30766674,  3.02943601,  3.47347219],\n       [ 0.29128692,  3.64944689,  2.91999511]])"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-05-03T13:31:42.727333100Z",
     "start_time": "2023-05-03T13:31:42.711333200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
